/**
 * Copyright 2011 Alex Vikulov (vikuloff@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package com.vvip.stockchart.series;


import com.vvip.stockchart.core.SeriesPaintInfo;
import com.vvip.stockhart.point.CustomPoint;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;

public class StockSeries extends AbstractSeries
{
	public enum ViewType
	{
		CANDLESTICK,
		BAR
	}
	private ViewType fViewType = ViewType.CANDLESTICK;
	
	private int fFallColor = Color.RED;
	private int fRiseColor = Color.GREEN;
	private int fOutlineColor = Color.BLACK;

	private final Paint fPaint = new Paint();
	
	private final RectF fBodyRect = new RectF();
	
	public StockSeries()
	{
		super(4);
	}


	public ViewType getViewType()
	{
		return fViewType;
	}
	
	public void setViewType(ViewType viewType)
	{
		fViewType = viewType;
	}
	
	public int getFallColor() {
		return fFallColor;
	}

	public void setFallColor(int fFallColor) {
		this.fFallColor = fFallColor;
	}

	public int getRiseColor() {
		return fRiseColor;
	}

	public void setRiseColor(int fRiseColor) {
		this.fRiseColor = fRiseColor;
	}

	public int getOutlineColor() {
		return fOutlineColor;
	}

	public void setOutlineColor(int fOutlineColor) {
		this.fOutlineColor = fOutlineColor;
	}
	
	protected void drawPoint(Canvas c, SeriesPaintInfo pinfo, float x1,float x2, CustomPoint p) 
	{

		this.fPaint.reset();
		
		double openPrice = p.Values[0];
		double closePrice = p.Values[1];
		
		boolean isFall = (closePrice < openPrice);
		
		float open = pinfo.getY(p.Values[0]);
		float close = pinfo.getY(p.Values[1]);
		float high = pinfo.getY(p.Values[2]);
		float low = pinfo.getY(p.Values[3]);
				
		float midX = (x1 + x2) / 2;
		
		if(getViewType() == ViewType.CANDLESTICK)
		{
			fBodyRect.set(x1, open, x2, close);
			fBodyRect.sort();
			
			fPaint.setColor(fOutlineColor);
			c.drawLine(midX, high, midX, low, fPaint);
			
			fPaint.setColor(isFall?fFallColor:fRiseColor);
			c.drawRect(fBodyRect, fPaint);
			
			//fPaint.setStyle(Style.FILL);
			// body
			
			fPaint.setStyle(Style.STROKE);
			fPaint.setColor(fOutlineColor);
			// outline
			c.drawRect(fBodyRect, fPaint);			
		}
		else if(getViewType() == ViewType.BAR)
		{
			fPaint.setColor(fOutlineColor);
			c.drawLine(midX, high, midX, low, fPaint);
			c.drawLine(x1, open, midX, open, fPaint);
			c.drawLine(midX, close, x2, close, fPaint);
		}
	}
	
}
